package com.manpower.ui;

import com.manpower.Receiver;
import com.manpower.utl.LocationController;
import com.manpower.view.ButtonBack;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FrameActivityGroup extends ActivityGroup {

	private static FrameActivityGroup sInstance;
	private LinearLayout mSwitcher;
	private TextView txtTitle;
	private ImageView backBtn;

	private Button mToFirstPage;
	private Button mToRegistrateButton;
	private Button mToUnCheckFileButton;
	private Button mToManualButton;
	private Button mToReportButton;
	private Button mToRegionStat;
	private LinearLayout mBar;

	TelephonyManager mTelePhonyMgr;
	private Receiver receiver;

	public static final int INITIATE_PAGE = 0;
	public static final int TO_ENREGISTRATION = 1;
	public static final int TO_UNCHECKFILE = 2;
	public static final int TO_MANUAL = 3;
	public static final int TO_REPORT = 4;
	public static final int TO_REGION_STAT = 5;

	public FrameActivityGroup() {
		super();
		sInstance = this;
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction()==KeyEvent.ACTION_DOWN) {
			new ExitDialog(FrameActivityGroup.this, R.style.custom_dialog)
					.show();
			return true;
		}
		return super.dispatchKeyEvent(event);
	}

	public static FrameActivityGroup getInstance() {
		if (sInstance == null) {
			new FrameActivityGroup();
		}
		return sInstance;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.activitygroup_frame);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.layout_activity_title);
		txtTitle = (TextView) (getWindow().getDecorView()
				.findViewById(R.id.txtTitle));
		backBtn = (ButtonBack) (getWindow().getDecorView()
				.findViewById(R.id.btnBack));

		initiate();
		switchActivity(INITIATE_PAGE);
	}

	@Override
	protected void onResume() {
		Animation animation = AnimationUtils
				.loadAnimation(this, R.anim.zoom_in);
		mBar.startAnimation(animation);
		super.onResume();
	}

	private void initiate() {
		LocationController.getInstance(this).startLocate();
		receiver = Receiver.registerReceiver(this);
		mSwitcher = (LinearLayout) findViewById(R.id.switcher);
		mToFirstPage = (Button)findViewById(R.id.first_page);
		mToManualButton = (Button) findViewById(R.id.manual);
		mToRegionStat = (Button) findViewById(R.id.regionstat);
		mToRegistrateButton = (Button) findViewById(R.id.enregistrate);
		mToReportButton = (Button) findViewById(R.id.report);
		mToUnCheckFileButton = (Button) findViewById(R.id.uncheckfile);
		mBar = (LinearLayout) findViewById(R.id.bar);

		mToFirstPage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				switchActivity(INITIATE_PAGE);
			}
		});
		
		mToManualButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				switchActivity(TO_MANUAL);
			}
		});
		mToRegionStat.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				switchActivity(TO_REGION_STAT);
			}
		});
		mToRegistrateButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				switchActivity(TO_ENREGISTRATION);
			}
		});
		mToReportButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				switchActivity(TO_REPORT);
			}
		});
		mToUnCheckFileButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				switchActivity(TO_UNCHECKFILE);
			}
		});
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new ExitDialog(FrameActivityGroup.this, R.style.custom_dialog)
						.show();
			}
		});
	}

	public void switchActivity(int id) {
		mSwitcher.removeAllViews();
		Intent intent = null;
		Window subActivity = null;
		switch (id) {
		case INITIATE_PAGE:
			intent = new Intent(FrameActivityGroup.this,
					FeatureListActivity.class);
			subActivity = getLocalActivityManager().startActivity(
					"initiate_page", intent);
			txtTitle.setText(R.string.app_name);
			break;
		case TO_ENREGISTRATION:
			intent = new Intent(FrameActivityGroup.this,
					EnRegistrationItemActivity.class);
			subActivity = getLocalActivityManager().startActivity(
					"to_enregistration", intent);
			txtTitle.setText(R.string.label_EnRegistration);
			break;
		case TO_UNCHECKFILE:
			intent = new Intent(FrameActivityGroup.this,
					UnCheckFileActivity.class);
			subActivity = getLocalActivityManager().startActivity(
					"to_uncheckfile", intent);
			txtTitle.setText(R.string.label_uncheck_file);
			break;
		case TO_MANUAL:
			intent = new Intent(FrameActivityGroup.this, ManualActivity.class);
			subActivity = getLocalActivityManager().startActivity("to_manual",
					intent);
			txtTitle.setText(R.string.label_manual);
			break;
		case TO_REPORT:
			intent = new Intent(FrameActivityGroup.this,
					ReportStatusActivity.class);
			subActivity = getLocalActivityManager().startActivity(
					"to_report_status", intent);
			txtTitle.setText(R.string.label_report_state);
			break;
		case TO_REGION_STAT:
			intent = new Intent(FrameActivityGroup.this,
					RegionalStatisticalActivity.class);
			subActivity = getLocalActivityManager().startActivity(
					"to_report_status", intent);
			txtTitle.setText(R.string.label_regional_statistical);
			break;

		}
		mSwitcher.addView(subActivity.getDecorView(), LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		LocationController.getInstance(this).stopLocate();
		unregisterReceiver(receiver);
	}

}
